package me.aartikov.navigationmethodssample.slice;


import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.navigationmethodssample.SampleApplication;
import me.aartikov.navigationmethodssample.ResourceTable;
import me.aartikov.navigationmethodssample.screens.TestSmallScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;


@RegisterScreen(TestSmallScreen.class)
public class TestMiFraction extends MiFraction {

//    Component mRootView;
    Text mCounterTextView;
    Button mForwardButton;
    Button mReplaceButton;
    Button mResetButton;
    Button mBackButton;
    Button mDoubleBackButton;

    private Navigator mNavigator = SampleApplication.getInstance().getNavigator();

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_mifraction_test, container, false);

//        mRootView = component.findComponentById(ResourceTable.Id_root_view);
        mCounterTextView = (Text) component.findComponentById(ResourceTable.Id_counter_text_view);
        mForwardButton = (Button) component.findComponentById(ResourceTable.Id_forward_button);
        mReplaceButton = (Button) component.findComponentById(ResourceTable.Id_replace_button);
        mResetButton = (Button) component.findComponentById(ResourceTable.Id_reset_button);
        mBackButton = (Button) component.findComponentById(ResourceTable.Id_back_button);
        mDoubleBackButton = (Button) component.findComponentById(ResourceTable.Id_double_back_button);
        TestSmallScreen screen = SampleApplication.getInstance().getScreenResolver().getScreen(this);
        int counter = screen.getCounter();
        mCounterTextView.setText(getFractionAbility().getString(ResourceTable.String_counter_template, counter));


        mForwardButton.setClickedListener(v -> mNavigator.goForward(new TestSmallScreen(counter + 1,ResourceTable.Id_fragment_container,screen.getGroup().toString())));
        mReplaceButton.setClickedListener(v -> mNavigator.replace(new TestSmallScreen(counter,ResourceTable.Id_fragment_container,screen.getGroup().toString())));
        mResetButton.setClickedListener(v -> mNavigator.reset(new TestSmallScreen(1,ResourceTable.Id_fragment_container,screen.getGroup().toString())));
        mBackButton.setClickedListener(v -> mNavigator.goBack());
        mDoubleBackButton.setClickedListener(v -> {
            mNavigator.goBack();        // Due to a command queue in OhosNavigator you can combine navigation methods arbitrarily.
            mNavigator.goBack();
        });

        return component;
    }

    @Override
    protected void setInitialFragmentIfRequired() {

    }


}
